package com.lineage.server.clientpackets;

import com.lineage.echo.ClientExecutor;
import com.lineage.server.command.executor.L1AllBuff;
import com.lineage.server.datatables.lock.CharBuffReading;
import com.lineage.server.datatables.lock.IpReading;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Location;
import com.lineage.server.model.L1Party;
import com.lineage.server.model.L1Teleport;
import com.lineage.server.model.skill.L1SkillMode;
import com.lineage.server.serverpackets.S_CharVisualUpdate;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SkillHaste;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.world.World;
import java.util.HashMap;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.concurrent.RejectedExecutionException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class C_CallPlayer extends ClientBasePacket {
  private static final Log _log = LogFactory.getLog(C_CallPlayer.class);
  
  public void start(byte[] decrypt, ClientExecutor client) {
    try {
      read(decrypt);
      L1PcInstance pc = client.getActiveChar();
      if (!pc.isGm())
        return; 
      String arg = readS();
      StringTokenizer stringtokenizer = new StringTokenizer(arg, ":");
      int mode = pc.getTempID();
      try {
        L1Location loc, gmloc;
        L1Party party;
        StringBuilder ipaddr;
        int x;
        StringBuilder macaddr;
        int y;
        short map;
        HashMap<Integer, L1PcInstance> pcs;
        Iterator<L1PcInstance> iterator;
        int mapid = Integer.parseInt(stringtokenizer.nextToken());
        String name = stringtokenizer.nextToken();
        if (name.isEmpty())
          return; 
        L1PcInstance target = World.get().getPlayer(name);
        if (target == null) {
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(73, name));
          return;
        } 
        switch (mode) {
          case 0:
            target.clearAllSkill();
            CharBuffReading.get().deleteBuff(target);
            stopSkill(target);
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(166, String.valueOf(String.valueOf(target.getName())) + " Buff清除!"));
            break;
          case 1:
            loc = L1Location.randomLocation(target.getLocation(), 1, 2, false);
            pc.set_showId(target.get_showId());
            L1Teleport.teleport(pc, loc.getX(), loc.getY(), target.getMapId(), pc.getHeading(), false);
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(166, "移动座标至指定人物身边: " + name));
            break;
          case 2:
            gmloc = L1Location.randomLocation(pc.getLocation(), 1, 2, false);
            L1Teleport.teleport(target, gmloc.getX(), gmloc.getY(), pc.getMapId(), target.getHeading(), false);
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(166, "召回指定人物: " + name));
            break;
          case 3:
            party = target.getParty();
            if (party == null)
              break; 
            x = pc.getX();
            y = pc.getY() + 2;
            map = pc.getMapId();
            pcs = new HashMap<>();
            pcs.putAll(party.partyUsers());
            if (pcs.isEmpty())
              return; 
            if (pcs.size() <= 0)
              return; 
            iterator = pcs.values().iterator();
            while (iterator.hasNext()) {
              L1PcInstance pc2 = iterator.next();
              try {
                L1Teleport.teleport(pc2, x, y, map, 5, true);
                pc2.sendPackets((ServerBasePacket)new S_SystemMessage("管理员召唤!"));
              } catch (Exception exception) {}
            } 
            pcs.clear();
            break;
          case 4:
            L1AllBuff.startPc(target);
            break;
          case 5:
            pc.sendPackets((ServerBasePacket)new S_SystemMessage(String.valueOf(String.valueOf(target.getName())) + " 踢除下线。"));
            target.getNetConnection().kick();
            break;
          case 6:
            ipaddr = target.getNetConnection().getIp();
            macaddr = target.getNetConnection().getMac();
            if (ipaddr != null)
              IpReading.get().add(ipaddr.toString(), "GM命令:L1PowerKick 封锁"); 
            if (macaddr != null)
              IpReading.get().add(macaddr.toString(), "GM命令:L1PowerKick 封锁"); 
            pc.sendPackets((ServerBasePacket)new S_SystemMessage(String.valueOf(String.valueOf(target.getName())) + " 封锁IP/MAC。"));
            target.getNetConnection().kick();
            break;
          case 7:
            IpReading.get().add(target.getAccountName(), "GM命令:L1AccountBanKick 封锁帐号");
            pc.sendPackets((ServerBasePacket)new S_SystemMessage(String.valueOf(String.valueOf(target.getName())) + " 帐号封锁。"));
            target.getNetConnection().kick();
            break;
          case 8:
            target.setCurrentHp(0);
            target.death(null);
            pc.sendPackets((ServerBasePacket)new S_SystemMessage(String.valueOf(String.valueOf(target.getName())) + " 人物死亡。"));
            break;
        } 
      } catch (RejectedExecutionException e) {
        String name = arg;
        if (name.isEmpty())
          return; 
        L1PcInstance target = World.get().getPlayer(name);
        if (target == null) {
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(73, name));
          return;
        } 
        L1Location loc = L1Location.randomLocation(target.getLocation(), 1, 2, false);
        L1Teleport.teleport(pc, loc.getX(), loc.getY(), target.getMapId(), pc.getHeading(), false);
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(166, "移动座标至指定人物身边: " + name));
      } 
    } catch (Exception exception) {
    
    } finally {
      over();
    } 
  }
  
  private void stopSkill(L1PcInstance pc) {
    int skillNum = 1;
    while (skillNum <= 220) {
      if (!L1SkillMode.get().isNotCancelable(skillNum) || pc.isDead())
        pc.removeSkillEffect(skillNum); 
      skillNum++;
    } 
    pc.curePoison();
    pc.cureParalaysis();
    skillNum = 998;
    while (skillNum <= 1027) {
      pc.removeSkillEffect(skillNum);
      skillNum++;
    } 
    skillNum = 3000;
    while (skillNum <= 3051) {
      if (!L1SkillMode.get().isNotCancelable(skillNum))
        pc.removeSkillEffect(skillNum); 
      skillNum++;
    } 
    if (pc.getHasteItemEquipped() > 0) {
      pc.setMoveSpeed(0);
      pc.sendPacketsAll((ServerBasePacket)new S_SkillHaste(pc.getId(), 0, 0));
    } 
    pc.removeSkillEffect(4000);
    pc.sendPacketsAll((ServerBasePacket)new S_CharVisualUpdate(pc));
  }
  
  public String getType() {
    return getClass().getSimpleName();
  }
}
